﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using phudieuhungthinh.DAL.Repositories;
using phudieuhungthinh.BL.Model;
using phudieuhungthinh.DAL;
using canthovieclam.Core.Utilities;
namespace phudieuhungthinh.BL
{
    public class CategoryImpl
    {
        #region Front end methods

        public List<CategoryMenu> GetCategoryMenus()
        {
            var categoryRepository = new CategoryRepository();
            var query = categoryRepository.GetAllCategory();
            List<CategoryMenu> menus = (from x in query
                                        where x.ParentId == 0
                                        orderby x.Orders
                                        select new CategoryMenu(x, query)).ToList();
            return menus;
        }

        public List<CategoryMenu> GetCategoryMenus(int productId)
        {
            var categoryRepository = new CategoryRepository();
            var query = categoryRepository.GetAllCategory();
            List<CategoryMenu> menus = (from x in query
                                        where x.ParentId == 0
                                        orderby x.Orders
                                        select new CategoryMenu(x, query, productId)).ToList();
            return menus;
        }


        public List<CategoryMenu> GetCategories(int cateId)
        {
            var categoryRepository = new CategoryRepository();
            var query = categoryRepository.GetAllCategory();
            List<CategoryMenu> menus = (from x in query
                                        where x.ParentId == 0
                                        orderby x.Orders
                                        select new CategoryMenu(cateId, x, query)).ToList();
            return menus;
        }
        #endregion

        #region Admin Methods
        public List<CategoryModel> GetCategoryModelList()
        {
            var categoryRepository = new CategoryRepository();
            List<CategoryModel> listCategory = (from x in categoryRepository.GetAllCategory()
                                                orderby x.Orders
                                                select new CategoryModel(x)).ToList();
            return listCategory;
        }

        public void AddCategory(CategoryModel categoryModel)
        {
            categoryModel.Url = StringUtil.RemoveSign4VietnameseString(categoryModel.Name.ToLower()).Replace(' ', '-');
            categoryModel.UniqueID = GuidUtils.NewSeqGuid();

            var categoryRepository = new CategoryRepository();
            categoryRepository.AddCategory(categoryModel.MapCategory());
            categoryRepository.Commit();
        }

        public CategoryModel GetCategoryModel(int categoryId)
        {
            var categoryRepository = new CategoryRepository();
            Category category = categoryRepository.GetSingleCategory(x => x.ID == categoryId);
            var categoryModel = new CategoryModel(category);
            return categoryModel;
        }

        public CategoryModel GetCategoryModel(string url)
        {
            var categoryRepository = new CategoryRepository();
            Category category = categoryRepository.GetSingleCategory(x => x.Url == url);
            var categoryModel = new CategoryModel(category);
            return categoryModel;
        }

        public void UpdateCategory(CategoryModel categoryModel)
        {
            var categoryRepository = new CategoryRepository();
            var category = categoryRepository.GetSingleCategory(x => x.ID == categoryModel.ID);
            category.Name = categoryModel.Name;
            category.Description = categoryModel.Description;
            
            categoryRepository.UpdateCategory(category);
            categoryRepository.Commit();
        }

        public void DeleteCategory(CategoryModel categoryModel)
        {
            ////Delete product categories of this category
            var productCategoriesImpl = new ProductCategoriesImpl();
            var productCategoriesRepository = new ProductCategoriesRepository();
            var currentProductCategories = productCategoriesImpl.GetProductCategoriesByCategory(categoryModel.ID);
            IList<ProductCategories> deleteItems = currentProductCategories.Select(current => new ProductCategories {CategoryID = current.CategoryID, ProductID = current.ProductID, UniqueID = current.UniqueID}).ToList();
            productCategoriesRepository.DeleteBatchProductCategories(deleteItems);
            productCategoriesRepository.Commit();

            var categoryRepository = new CategoryRepository();
            categoryRepository.DeleteCategory(categoryModel.MapCategory());
            categoryRepository.Commit();
        }
        #endregion
    }
}
